package com.cloud.hotline.api.provider;

import java.util.Date;

import com.cloud.hotline.api.bean.HotlineCalculateForm;
import com.cloud.hotline.api.consts.HotLineConsts;
import com.cloud.hotline.common.utils.DateHelper;

public class HotlineUnitCalculateProvider {
	private static final String SELECT = "select @rownum :=@rownum + 1 AS pm, mc, jrzs from ("
			+ "select (SELECT @rownum := 0) r, unit_name as mc, sum(c) as jrzs from (select unit_name, 1 as c from to_hotline_record where delete_flag = 0 ";

	private static final String GROUP = ") t group by unit_name order by jrzs desc) t";

	public String caculate(HotlineCalculateForm form) {
		StringBuilder sqlBuilder = new StringBuilder(SELECT);
		// 考核周期：当天
		Date now = new Date();
		sqlBuilder.append(" and state_date >= '" + DateHelper.dateTimeToStr(DateHelper.getDateBegin(now)) + "'");
		sqlBuilder.append(" and state_date <= '" + DateHelper.dateTimeToStr(DateHelper.getDateEnd(now)) + "'");
		// 数据类型 -- 部门承办
		sqlBuilder.append(" and data_type_id = " + HotLineConsts.DATA_TYPE_UNIT_UNDERTAKE);
		sqlBuilder.append(GROUP);
		return sqlBuilder.toString();
	}
}
